package com.thinkfit.jy.activity;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.common.lib.bean.DeviceInfoBean;
import com.common.lib.bean.VideoListBean;
import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.manager.DataManager;
import com.common.lib.utils.BaseUtils;
import com.github.mikephil.charting.charts.LineChart;
import com.thinkfit.jy.R;
import com.thinkfit.jy.dialog.AppPopupWindow;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.DateUtils;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.FastBlurUtility;
import com.thinkfit.jy.utils.LineChartUtils;
import com.thinkfit.jy.utils.MotionDataUtils;
import com.thinkfit.jy.utils.ShowSpeedViewUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;
import com.thinkfit.jy.utils.ThemeUtils;
import com.thinkfit.jy.utils.Utils;
import com.thinkfit.jy.view.CircleBarView;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ProceduralTrainInfoActivity extends MainActivity{

    private int[] colors;
    VideoListBean videoListBean;
    private int mProgressMargin;
    LinearLayout llCrnter,progressView;
    ConstraintLayout clMain;
    TextView tvTime,tvKcalTip,tvKcal,tvDistance,tvDistanceTip,tvSpeed,tvSpeedTip,tvSpm,tvSpmTip;
    CircleBarView circleBar;
    int time = 0;

    private boolean isShowLine = false;
    LineChartUtils lineChartUtils = new LineChartUtils(this);;
    LineChartUtils lineChartUtils1 = new LineChartUtils(this);;
    LineChartUtils lineChartUtils2 = new LineChartUtils(this);;
    LineChartUtils lineChartUtils3 = new LineChartUtils(this);;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_procedural_train_info;
    }

    @Override
    public void init() {
        videoListBean = (VideoListBean) getIntent().getSerializableExtra(Constants.BUNDLE_EXTRA);
        clMain = findViewById(R.id.cl_main);
        llCrnter = findViewById(R.id.ll_center);
        progressView = findViewById(R.id.progressView);
        tvTime = findViewById(R.id.tv_video_time);
        tvKcal = findViewById(R.id.tv_kcal);
        tvKcalTip = findViewById(R.id.tv_kcal_tip);
        tvDistance = findViewById(R.id.tv_distance);
        tvDistanceTip = findViewById(R.id.tv_distance_tip);
        tvSpeed = findViewById(R.id.tv_speed);
        tvSpeedTip = findViewById(R.id.tv_speed_tip);
        tvSpm = findViewById(R.id.tv_spm);
        tvSpmTip = findViewById(R.id.tv_spm_tip);
        circleBar = findViewById(R.id.circleBar);
        colors = new int[]{getResources().getColor(R.color.color_45B4FF),
                getResources().getColor(R.color.color_36D16F),
                getResources().getColor(R.color.color_FFBB00),
                getResources().getColor(R.color.color_FF8A05),
                getResources().getColor(R.color.color_FF5959),
                getResources().getColor(R.color.color_AA51F0)};
        setViewsOnClickListener(R.id.ivBack,R.id.ivHome,R.id.ivAvatar,R.id.tvUseName,R.id.tvNotLogin,R.id.iv_line,
                R.id.llStart,R.id.ivSlopMinus,R.id.ivSlopAdd,R.id.iv_app,R.id.ivBluetooth,R.id.ivSound);
        System.out.println("ddddddddddddddddddddddd   videoListBean.size : "+videoListBean.getBrustlist().size());

        for (DeviceInfoBean deviceInfoBean : videoListBean.getBrustlist()){
            System.out.println("ddddddddddddddddddddddd   "+videoListBean.getBrustlist().toString());
            setView(llCrnter,deviceInfoBean,true);
            setView(progressView,deviceInfoBean,false);
        }
        if(DataManager.Companion.getInstance().getUnit()){
            setText(tvDistanceTip,getString(R.string.app_distance_mi));
            setText(R.id.tv_speed_tip,getString(R.string.app_pace)+"(min/mi)");
        }else {
            setText(tvDistanceTip,getString(R.string.app_distance));
            setText(R.id.tv_speed_tip,getString(R.string.app_pace)+"(min/km)");
        }


        if(MotionDataUtils.getInstance().mCurrentDeviceType.equals(DeviceTypeUtils.SPINNING)
                ||MotionDataUtils.getInstance().mCurrentDeviceType.equals(DeviceTypeUtils.ELLIPTICAL_MACHINE)){
            //动感单车或椭圆机
            initSpinning();
        } else if (MotionDataUtils.getInstance().mCurrentDeviceType.equals(DeviceTypeUtils.ROWING_MACHINE)){
            //划船机
            initRowingMachine();
        }else if(MotionDataUtils.getInstance().mCurrentDeviceType.equals(DeviceTypeUtils.TREADMILL)){
            //跑步机
            initTreadmill();
        }
        LineChart lc1 = findViewById(R.id.lc_1);
        LineChart lc2 = findViewById(R.id.lc_2);
        LineChart lc3 = findViewById(R.id.lc_3);
        LineChart lc4 = findViewById(R.id.lc_4);
        lineChartUtils.initView(lc1);
        lineChartUtils1.initView(lc2);
        lineChartUtils2.initView(lc3);
        lineChartUtils3.initView(lc4);
    }

    @Override
    public void onResume() {
        super.onResume();
        clMain.setBackgroundResource(ThemeUtils.getInstance().getMainTheme());
        ShowSpeedViewUtils.getInstance().initView(getContext().getApplicationContext());
        SportsStatusBarViewUtils.getInstance().initView(getContext().getApplicationContext());
    }

    @Override
    protected void onStop() {
        super.onStop();
//        ShowSpeedViewUtils.getInstance().goneLayout();
    }

    /**
     * 初始化动感单车
     */
    public void initSpinning(){
        setText(tvSpmTip,getString(R.string.app_speed_rate)+"(rpm)");
        setText(R.id.tv,getString(R.string.app_resistance));
        setViewInvisible(R.id.ivSpeedAdd,R.id.ivSpeedMinus);
        circleBar.setUnitText(getString(R.string.app_rpm));
        circleBar.setValueText("0");
        circleBar.setText(getString(R.string.app_big_rpm)+getString(R.string.app_target_xxx,
                videoListBean.getBrustlist().get(0).getMinrpm()+"-"+videoListBean.getBrustlist().get(0).getMaxrpm()));
        setText(R.id.tv_lc_slop,getString(R.string.app_resistance));
        setText(R.id.tv_lc_spm,getString(R.string.app_speed_rate));
    }

    /**
     * 初始化划船机
     */
    public void initRowingMachine(){
        setText(tvSpmTip,getString(R.string.app_pulp_rate)+"(spm)");
        setText(tvSpeedTip,getString(R.string.app_pace)+"(min/500m)");
        setText(R.id.tv,getString(R.string.app_resistance));
        setViewInvisible(R.id.ivSpeedAdd,R.id.ivSpeedMinus);
        circleBar.setUnitText("spm");
        circleBar.setValueText("0");
        circleBar.setText(getString(R.string.app_big_spm)+getString(R.string.app_target_xxx,
                videoListBean.getBrustlist().get(0).getMinrpm()+"-"+videoListBean.getBrustlist().get(0).getMaxrpm()));
        setText(R.id.tv_lc_slop,getString(R.string.app_resistance));
        setText(R.id.tv_lc_spm,getString(R.string.app_pulp_rate));
    }

    /**
     * 初始化跑步机
     */
    public void initTreadmill(){
        setText(tvSpmTip,getString(R.string.app_step_rate_spm));
        setViewsOnClickListener(R.id.ivSpeedAdd,R.id.ivSpeedMinus);
        circleBar.setUnitText("spm");
        circleBar.setValueText("0");
        circleBar.setText(getString(R.string.app_big_spm)+getString(R.string.app_target_xxx,
                videoListBean.getBrustlist().get(0).getMinrpm()+"-"+videoListBean.getBrustlist().get(0).getMaxrpm()));
        setText(R.id.tv_lc_slop,getString(R.string.app_slope));
        setText(R.id.tv_lc_spm,getString(R.string.app_big_spm));
    }

    public void resetInfo(SerialPortManager.CurrentInfo info) {
        String distance="0.0",spm="0",kcal="0",slop = "0",pace="00:00";
        float valY = 0f,valY2 = 0f,speed = 0f;

        if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.TREADMILL)){
            kcal = Utils.getNewFloat(info.kcal * 1.0f / 1000);
            distance =  Utils.getNewFloat2(info.distance / 1000f);
            if(DataManager.Companion.getInstance().getUnit()){
                setText(R.id.tvSpeed, Utils.getNewFloat(info.speed / 16f));
                valY = Float.parseFloat(Utils.getNewFloat(info.speed / 16f));
                speed = info.speed / 16f;
            }else {
                setText(R.id.tvSpeed, Utils.getNewFloat(info.speed / 10f));
                valY = Float.parseFloat(Utils.getNewFloat(info.speed / 10f));
                speed = info.speed / 10f;
            }
            pace = (info.speed == 0 ? "0" : Utils.getNewFloat2(60f / (info.speed / 16f)));
            slop = Utils.getNewFloat(info.slop);
            spm = String.valueOf(0);
            valY2 = Float.parseFloat(Utils.getNewFloat(info.slop));
        }else if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.SPINNING)){
            kcal = Utils.getNewFloat(info.kcal * 0.1f);
            distance =  Utils.getNewFloat2(info.distance / 1000f);
            setText(R.id.tvSpeed, Utils.getNewFloat(info.speed*0.01f));
            speed = info.speed*0.01f;
            slop = String.valueOf(info.slop);
            spm = String.valueOf(info.rpm);
            if (info.speed <= 0) {
                pace = "00:00";
            } else {
                pace = DateUtils.getInstance().speedKmMin(info.speed);
            }
            valY2 = info.slop;
            valY = Float.parseFloat(Utils.getNewFloat(info.speed*0.01f));
        }else if (MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.ROWING_MACHINE)){
            kcal = Utils.getNewFloat(info.kcal * 0.1f);
            distance =  Utils.getNewFloat2(info.distance / 1000f);
            if(DataManager.Companion.getInstance().getUnit()){
                setText(R.id.tvSpeed, Utils.getNewFloat(info.speed / 16f));
                valY = Float.parseFloat(Utils.getNewFloat(info.speed / 16f));
                speed = info.speed / 160f;
            }else {
                setText(R.id.tvSpeed, Utils.getNewFloat(info.speed / 10f));
                valY = Float.parseFloat(Utils.getNewFloat(info.speed / 10f));
                speed = info.speed / 100f;
            }
            if (info.speed == 0) {
                pace = "00:00";
            } else {
                pace = DateUtils.getInstance().speedAllocationConvertInto(info.speed);
            }
            slop = String.valueOf(info.slop);
            spm = String.valueOf(info.rpm);
            valY2 = Float.parseFloat(String.valueOf(info.slop));

        }

        setText(tvDistance, distance);
        setText(tvSpm, spm);
        setText(tvKcal, kcal);
        setText(tvSpeed, pace);
        setText(R.id.tvSlope, slop);

        if(time < info.time){
            System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa  time  "+info.time);
            time = info.time;
            int minute = (info.time / 60) % 60;
            int second = info.time % 60;
            tvTime.setText(BaseUtils.StaticParams.getNewText(minute) + ":"
                    + BaseUtils.StaticParams.getNewText(second));
            int rpm;
            if(DeviceTypeUtils.getInstance().isTreadmill()){
                double rand = Math.random()*10;
                rand=(rand-5)*0.01+1;
                rpm = (int) Math.ceil(speed*60/3.6*rand);
            }else {
                rpm = (int) Math.ceil(speed*2.5);
            }

            System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa  rpm  "+rpm+"   speed : "+ speed);
            if(DeviceTypeUtils.getDeviceType() == 2 || DeviceTypeUtils.getDeviceType() == 5){
                setProgress(llCrnter,v2s,videoListBean.getBrustlist(),time,rpm);
                setProgress(progressView,progressV2s,videoListBean.getBrustlist(),time,rpm);
            }else {
                setProgress(llCrnter,v2s,videoListBean.getBrustlist(),time,rpm);
                setProgress(progressView,progressV2s,videoListBean.getBrustlist(),time,rpm);

            }

            if(!DeviceTypeUtils.getInstance().isTreadmill()){
                setSpeedAndSlop(info);
            }
            lineChartUtils.invalidate(time,valY);
            lineChartUtils1.invalidate(time,info.heart);
            lineChartUtils2.invalidate(time,valY2);
            lineChartUtils3.invalidate(time,info.count);
        }

    }

    /**
     * 设置视频播放节点的速度和坡度
     */
    public void setSpeedAndSlop(SerialPortManager.CurrentInfo info){
        for (DeviceInfoBean videoSectionBean : videoListBean.getBrustlist()){
            if(time > videoSectionBean.getStartime() && time <= videoSectionBean.getEndtime()){
                int speed = (int) (videoSectionBean.getSpeed()*10);
                int drag = videoSectionBean.getDrap();
                if(DeviceTypeUtils.getInstance().isTreadmill()){
                    if(info.speed == speed && info.slop == drag){
                        return;
                    }
                }else {
                    if(info.slop == drag){
                        return;
                    }
                }

                if(DeviceTypeUtils.isConnect()){
                    if(DeviceTypeUtils.getInstance().isTreadmill()){
                        if(DeviceTypeUtils.connectDeviceType() == 0){
                            BluetoothOrder.getInstance().setTreadmillSpeedAndSlope(speed,drag);
                        }else {
                            SerialPortManager.getInstance().setSpeedSlop(speed,drag);
                        }

                    }else {
                        if(DeviceTypeUtils.connectDeviceType() == 0){
                            BluetoothOrder.getInstance().setNowelAndSlope(drag,speed);
                        }else{
                            SerialPortManager.getInstance().setResistanceSlop(drag,speed);

                        }

                    }
                }else {
                    ToastUtils.showShort(getString(R.string.app_device_connect));
                }

                System.out.println("ddddddddddddddddddddddd  currentVideoSectionBean : "+videoSectionBean.toString());
                break;
            }
        }
        System.out.println("ddddddddddddddddddddddd  time : "+time);
    }

    @Override
    public void onReceive(HashMap<String, Object> map) {
        if (map == null) {
            return;
        }
        if (map.containsKey(EventBusEvent.TIME_CHANGE)) {
            resetDate();
        }else if(map.containsKey(EventBusEvent.KILL_ACTIVITY)){
            finish();
        }else {
            if(map.containsKey(EventBusEvent.START_RUN)){
                showStartBtn(true);
                ShowSpeedViewUtils.getInstance().initView(getApplicationContext());
            }else if(map.containsKey(EventBusEvent.STOP_RUN)){
                System.out.println("BluetoothOrder ------------->  STOP_RUN : 2");
                showStartBtn(false);
                BluetoothOrder.getInstance().uploadSportInfo(true);
                initWidget();
                ShowSpeedViewUtils.getInstance().goneLayout();
            }
            super.onReceive(map);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()){
            case R.id.ivBack:
                if (MotionDataUtils.getInstance().isRun()) {
                    if(DeviceTypeUtils.connectDeviceType() == 0){
                        BluetoothOrder.getInstance().bluetoothOrderWrite(BluetoothOrder.EQUIPMENT_STOP);
                    }else {
                        SerialPortManager.getInstance().stopRun();
                    }
                    finish();
                    return;
                }
                if (DataManager.Companion.getInstance().getMainStyle() == 3) {
                    DataManager.Companion.getInstance().setObject(FastBlurUtility.getBlurBackgroundDrawer(this));
                }
                intent = new Intent(this, DialogActivity.class);
                intent.putExtra(Constants.BUNDLE_EXTRA, DialogActivity.END_SPORT_BACK);
                startActivity(intent);
                break;
            case R.id.ivHome:
                if (DataManager.Companion.getInstance().getMainStyle() == 3) {
                    DataManager.Companion.getInstance().setObject(FastBlurUtility.getBlurBackgroundDrawer(this));
                }
                intent = new Intent(this, DialogActivity.class);
                intent.putExtra(Constants.BUNDLE_EXTRA, DialogActivity.END_SPORT_TO_HOME);
                startActivity(intent);
                break;
            case R.id.iv_line:
                if(isShowLine){
                    isShowLine = false;
                    setViewGone(R.id.cl_line);
                }else {
                    isShowLine = true;
                    setViewVisible(R.id.cl_line);
                }
                break;
            case R.id.ivSlopAdd:
                if (!SerialPortManager.getInstance().isConnected()&&!BluetoothOrder.getInstance().isConnect()) {
                    showToast(R.string.app_device_connect);
                    return;
                }
                resetSlop(true);
                break;
            case R.id.ivSlopMinus:
                if (!SerialPortManager.getInstance().isConnected()&&!BluetoothOrder.getInstance().isConnect()) {
                    showToast(R.string.app_device_connect);
                    return;
                }
                resetSlop(false);
                break;
            case R.id.ivSpeedAdd:
                if (!SerialPortManager.getInstance().isConnected()&&!BluetoothOrder.getInstance().isConnect()) {
                    showToast(R.string.app_device_connect);
                    return;
                }
                resetSpeed(true);
                break;
            case R.id.ivSpeedMinus:
                if (!SerialPortManager.getInstance().isConnected()&&!BluetoothOrder.getInstance().isConnect()) {
                    showToast(R.string.app_device_connect);
                    return;
                }
                resetSpeed(false);
                break;
            case R.id.iv_app:
//                AppPopupWindow.getInstance().initView(getApplicationContext(), getAppList());
                break;
            default:
                super.onClick(v);
                break;
        }

    }
    ArrayList<View> v2s = new ArrayList<>();
    ArrayList<View> progressV2s = new ArrayList<>();

    public void setView(LinearLayout llChart, DeviceInfoBean deviceInfoBean,boolean isHight){
        mProgressMargin = BaseUtils.StaticParams.dp2px(getContext(), 2);
        ViewGroup.LayoutParams layoutParams = llChart.getLayoutParams();
        int maxHight = layoutParams.height;
        int maxWidth = layoutParams.width;
//        System.out.println("ddddddddddddddddddddddd   maxHight : "+maxHight);
//        System.out.println("ddddddddddddddddddddddd   maxWidth : "+maxWidth);

        View item = LayoutInflater.from(getContext()).inflate(R.layout.item_histogram_info, null);
        View v1 = item.findViewById(R.id.v1);
        View v2 = item.findViewById(R.id.v2);
        ViewGroup.LayoutParams layoutParams1 = v1.getLayoutParams();
        ViewGroup.LayoutParams layoutParams2 = v2.getLayoutParams();
//        System.out.println("ddddddddddddddddddddddd   deviceInfoBean.getHeight() : "+deviceInfoBean.getHeight());
//        System.out.println("ddddddddddddddddddddddd   deviceInfoBean.getWidth() : "+deviceInfoBean.getWidth());
//        System.out.println("ddddddddddddddddddddddd   Math : "+Math.round(maxHight*deviceInfoBean.getHeight()));
//        System.out.println("ddddddddddddddddddddddd   Math2222  : "+(layoutParams1 == null));

        if(isHight){
            layoutParams1.height = Math.round(maxHight*deviceInfoBean.getHeight());
        }else {
            layoutParams1.height = maxHight;
            layoutParams2.height = ViewGroup.LayoutParams.MATCH_PARENT;
        }
        layoutParams1.width = Math.round((maxWidth-20)*deviceInfoBean.getWidth());
        v1.setLayoutParams(layoutParams1);


        llChart.addView(item);
        if(isHight){
            v2s.add(v2);
        }else {
            progressV2s.add(v2);
        }

    }

    public void setProgress(LinearLayout llChart,List<View> views,List<DeviceInfoBean> list,int count,int rpm){
        ViewGroup.LayoutParams layoutParams = llChart.getLayoutParams();
        int maxHight = layoutParams.height;
        int maxWidth = layoutParams.width;

        int viewNum = 0;
        for(int i=0;i<list.size();i++){
            DeviceInfoBean deviceInfoBean = list.get(i);
            if(deviceInfoBean.getEndtime()< count){

            }else if(deviceInfoBean.getStartime()<= count && count<deviceInfoBean.getEndtime()){
                viewNum = i;
                ViewGroup.LayoutParams lp1 = views.get(viewNum).getLayoutParams();
                views.get(viewNum).setBackgroundColor(setColor(list.get(viewNum).getWatt()));
                float dx = (list.get(viewNum).getEndtime() - list.get(viewNum).getStartime());
                int num = count - list.get(viewNum).getStartime();
                float v = (maxWidth * list.get(viewNum).getWidth())/dx;

                lp1.width = Math.round(v*num);
                views.get(viewNum).setLayoutParams(lp1);
                System.out.println("ddddddddddddddddddddddd   rpm : "+rpm+"   "+list.get(viewNum).getMinrpm());
                circleBar.setProgress((rpm-list.get(viewNum).getMinrpm()) > 0? (rpm-list.get(viewNum).getMinrpm()):0 );
                circleBar.setText(deviceType()+" "+getString(R.string.app_target_xxx,
                        videoListBean.getBrustlist().get(i).getMinrpm()+"-"+videoListBean.getBrustlist().get(i).getMaxrpm()));
//                v2s.get(i).setBackgroundColor(setColor(deviceInfoBean.getWatt()));
//                float dx = ((deviceInfoBean.getEndtime() - deviceInfoBean.getStartime())/100f);
//                int num = count - deviceInfoBean.getStartime();
//                System.out.println("ddddddddddddddddddddddd   dx  : "+dx);
//                System.out.println("ddddddddddddddddddddddd   num  : "+num);
//                System.out.println("ddddddddddddddddddddddd   width  : "+(lp1.width + (dx*num)));
//                lp1.width = Math.round(dx*num);
            }else {
//                lp1.width = 0;
            }

        }
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa  rpm1  "+rpm);
        circleBar.setValueText(""+rpm);

    }

    public String deviceType(){
        String type = null;
        if(DeviceTypeUtils.getDeviceType() == 1||DeviceTypeUtils.getDeviceType() == 6){
            type = getString(R.string.app_big_rpm);
        }else if(DeviceTypeUtils.getDeviceType() == 2||DeviceTypeUtils.getDeviceType() == 3){
            type = getString(R.string.app_big_spm);
        }else if(DeviceTypeUtils.getDeviceType() == 5){
            type = getString(R.string.app_big_spm);
        }
        return type;
    }
    public int setColor(int wait){
        if(wait<60){
            return colors[0];
        }else if(60<=wait && wait<=80){
            return colors[1];
        }else if(80<wait && wait<100){
            return colors[2];
        }else if(100<=wait && wait <110){
            return colors[3];
        }else if(110<=wait && wait <=120){
            return colors[4];
        }else {
            return colors[5];
        }

    }

    /**
     * 设置速度
     * @param isAdd 判断是否是增加
     */
    private void resetSpeed(boolean isAdd) {
        TextView tvSpeed = (TextView) findViewById(R.id.tvSpeed);
        String textById = tvSpeed.getText().toString().trim();
        if(textById.length()>=5){
            return;
        }
        BigDecimal speed = new BigDecimal(getTextById(R.id.tvSpeed));
        if (isAdd) {
            speed = speed.add(new BigDecimal("0.1"));
        } else {
            speed = speed.subtract(new BigDecimal("0.1"));
        }
        if (speed.compareTo(BigDecimal.ZERO) < 0) {
            return;
        }
        int slop = MotionDataUtils.getInstance().getCurrentInfo().slop;
        if(DeviceTypeUtils.connectDeviceType() == 0){
            if(DeviceTypeUtils.getInstance().isTreadmill()){
                BluetoothOrder.getInstance().setTreadmillSpeedAndSlope(speed.intValue(),slop);
            }else {
                BluetoothOrder.getInstance().setNowelAndSlope(speed.intValue(),slop);
            }
        }else {
            SerialPortManager.getInstance().setSpeedSlop(speed.multiply(BigDecimal.TEN).intValue(), slop);
        }

    }

    /**
     * 设置坡度
     * @param isAdd 判断是否是增加
     */
    private void resetSlop(boolean isAdd) {
        BigDecimal value = new BigDecimal(getTextById(R.id.tvSlope));
        if (isAdd) {
            value = value.add(new BigDecimal("1"));
        } else {
            value = value.subtract(new BigDecimal("1"));
        }
        if (value.compareTo(BigDecimal.ZERO) < 0) {
            return;
        }
        if(DeviceTypeUtils.connectDeviceType() == 0){
            if (MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.TREADMILL)){
                if (value.intValue() < 0) {
                    return;
                }
                int speed = MotionDataUtils.getInstance().getCurrentInfo().speed;
                BluetoothOrder.getInstance().setTreadmillSpeedAndSlope(speed,value.intValue());

            }else {

                if (value.intValue() < 0 || value.intValue() > 32) {
                    return;
                }
                int slop = MotionDataUtils.getInstance().getCurrentInfo().slop;
                BluetoothOrder.getInstance().setNowelAndSlope(value.intValue(), slop);
            }
        }else {
            if (MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.TREADMILL)) {
                if (value.intValue() < 0) {
                    return;
                }
                int speed = MotionDataUtils.getInstance().getCurrentInfo().speed;
                SerialPortManager.getInstance().setSpeedSlop(speed, value.intValue());
            } else {
                if (value.intValue() < 8 || value.intValue() > 32) {
                    return;
                }
                int slop = MotionDataUtils.getInstance().getCurrentInfo().slop;
                SerialPortManager.getInstance().setResistanceSlop(value.intValue(), slop);
            }
        }
    }

    public ArrayList<AppUtils.AppInfo> getAppList(){
        ArrayList<AppUtils.AppInfo> list1 = new ArrayList<>();
        PackageManager pm = this.getPackageManager();
        List<PackageInfo> packages = pm.getInstalledPackages(0);
        int size = packages.size();
        for (int i = 0; i < size; i++) {
            PackageInfo packageInfo = packages.get(i);
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0 ||
                    packageInfo.packageName.equals(this.getPackageName())) {
                continue;
            }
            list1.add(new AppUtils.AppInfo(
                    packageInfo.packageName,
                    packageInfo.applicationInfo.loadLabel(pm).toString(),
                    packageInfo.applicationInfo.loadIcon(pm),
                    "",
                    packageInfo.versionName,
                    packageInfo.versionCode,
                    false
            ));
        }
        return list1;
    }


    public void initWidget(){
        time = 0;
        setText(tvTime, "00:00");
        setText(tvDistance, "0.0");
        setText(tvSpm, "0.0");
        setText(tvKcal, "00");
        setText(tvSpeed, "0.0");
//        setText(R.id.tvSlope, "0");
//        setText(R.id.tvSpeed, "0.0");
        llCrnter.removeAllViews();
        progressView.removeAllViews();
        for (DeviceInfoBean deviceInfoBean : videoListBean.getBrustlist()){
            System.out.println("ddddddddddddddddddddddd   "+videoListBean.getBrustlist().toString());
            setView(llCrnter,deviceInfoBean,true);
            setView(progressView,deviceInfoBean,false);
        }
        if(DeviceTypeUtils.connectDeviceType() == 0){
            BluetoothOrder.getInstance().closeCurrentInfo();
        }else {
            SerialPortManager.getInstance().closeCurrentInfo();
        }
        lineChartUtils.clearValues();
        lineChartUtils1.clearValues();
        lineChartUtils2.clearValues();
        lineChartUtils3.clearValues();
    }

}
